<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr-left" :style="comStyle" @click="toLeft"
      ><i class="el-icon-arrow-left"></i
    ></span>
    <span class="iconfont arr-right" :style="comStyle" @click="toRight"
      ><i class="el-icon-arrow-right"></i
    ></span>
    <span class="cat-name" :style="comStyle">{{ catName }}</span>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0,
      titleFontSize: 0
    };
  },
  created() {
    //在组件创建完成后，进行回调函数的注册
    this.$socket.registerCallBack("hotData", this.getData);
  },
  computed: {
    catName() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.currentIndex].name;
      }
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px"
      };
    },
    ...mapState(["theme"])
  },

  mounted() {
    this.initChart();
    this.$socket.send({
      action: "getData",
      socketType: "hotData",
      chartName: "hot",
      value: ""
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },

  watch: {
    theme() {
      this.chartInstance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  },

  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("hotData");
  },

  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref, this.theme);
      const initOption = {
        title: {
          text: "▎ 热销商品的占比",
          left: 20,
          top: 20
        },
        legend: {
          left: "5%",
          bottom: "15%",
          orient: "vertical",
          icon: "circle"
        },
        tooltip: {
          show: true,
          formatter: arg => {
            const thirdCategory = arg.data.children;
            //三级分类所有数值总和
            let total = arg.data.value;
            let retStr = "";
            thirdCategory.forEach(item => {
              retStr += `
                ${item.name}:${parseInt((item.value / total) * 100) + "%"}
                <br>
              `;
            });
            return retStr;
          }
        },
        series: [
          {
            type: "pie",
            label: {
              show: true
            },
            //高亮时状态
            emphasis: {
              label: {
                show: true
              },
              labelLine: {
                show: false
              }
            }
          }
        ]
      };
      this.chartInstance.setOption(initOption);
    },
    getData(ret) {
      // const { data: res } = await this.$http.get("hotproduct");
      this.allData = ret;
      this.updateChart();
    },
    updateChart() {
      const legendData = this.allData[this.currentIndex].children.map(item => {
        return item.name;
      });
      const seriesData = this.allData[this.currentIndex].children.map(item => {
        return {
          name: item.name,
          value: item.value,
          children: item.children
        };
      });
      const dataOption = {
        legend: {
          data: legendData
        },
        series: [
          {
            data: seriesData
          }
        ]
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          itemWidth: this.titleFontSize / 2,
          itemHeight: this.titleFontSize / 2,
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        },
        series: [
          {
            radius: this.titleFontSize * 4.5,
            center: ["50%", "50%"]
          }
        ]
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    toLeft() {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updateChart();
    },
    toRight() {
      this.currentIndex++;
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updateChart();
    }
  }
};
</script>
<style lang="less" scoped>
.arr-left {
  position: absolute;
  left: 15%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.arr-right {
  position: absolute;
  right: 15%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.cat-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: white;
}
</style>
